<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\Actionlog;
use app\common\model\Adminlog;
use think\Controller;
use think\Request;

class Log extends AdminBase
{
    private static $AdminLogModel = null;
    private static $ActionLogModel = null;
    /**
     * 自动实例化模型对象
     * Log constructor.
     * @param Request|null $request
     */
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        if (self::$AdminLogModel==null){
            self::$AdminLogModel=new Adminlog();
        }
        if (self::$ActionLogModel==null){
            self::$ActionLogModel=new Actionlog();
        }
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function lists()
    {
        $get = $_GET;
        unset($get['/admin/log/lists']);
        $where=[];
        if (isset($get['username'])&&!empty($get['username'])){
            $where['a.username']=['like','%'.trim($get['username']).'%'];
        }
        $lists = self::$AdminLogModel->getAllData($where,$get);
        $this->assign('lists',$lists);
        return $this->fetch();
    }

    /**
     * 清空登录日志
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function allDel(){
        if (\request()->isAjax()){
            $allData = self::$AdminLogModel->select();
            if ($allData!=null){
                foreach ($allData as $v){
                    self::$AdminLogModel->where('log_id',$v['log_id'])->delete();
                }
                $res = true;
            }else{
                $res =false;
            }
            if ($res){
                return $this->resMessage(1,'操作成功');
            }else{
                return $this->resMessage(0,'操作失败');
            }
        }else{
            abort(404);
        }
    }

    /**
     * 操作日志查看
     * @return mixed
     */
    public function actionLog(){
        $get = $_GET;
        unset($get['/admin/log/actionLog']);
        $where=[];
        if (isset($get['username'])&&!empty($get['username'])){
            $where['a.username']=['like','%'.trim($get['username']).'%'];
        }
        $lists = self::$ActionLogModel->getAllData($where,$get);
        $this->assign('lists',$lists);
        return $this->fetch();
    }

    /**
     * 清空操作日志
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function allActionDel(){
        if (\request()->isAjax()){
            $allData = self::$ActionLogModel->select();
            if ($allData!=null){
                foreach ($allData as $v){
                    self::$ActionLogModel->where('action_log_id',$v['action_log_id'])->delete();
                }
                $res = true;
            }else{
                $res =false;
            }
            if ($res){
                return $this->resMessage(1,'操作成功');
            }else{
                return $this->resMessage(0,'操作失败');
            }
        }else{
            abort(404);
        }
    }

}
